package com.ky.manage.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.ky.manage.R;

/**
 * 通用dialog基类
 *
 * @author zhaoyan
 * @date 2019/11/07.
 */
public abstract class AbstractBaseDialog extends DialogFragment
        implements DialogInterface.OnKeyListener {

    private static final float DEFAULT_DIM = 0.2f;
    private static final boolean KEY_BACK_DISMISS = true;
    private final String TAG = AbstractBaseDialog.class.getSimpleName();
    private OnDialogKeyBackListener mOnDialogKeyBackListener;

    protected abstract int getLayoutRes();

    protected abstract void bindView(View v);

    protected abstract View getDialogView();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, getDialogThemeStyleRes());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = null;
        if (getLayoutRes() > 0) {
            view = inflater.inflate(getLayoutRes(), container, false);
        }
        if (getDialogView() != null) {
            view = getDialogView();
        }
        bindView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //去除Dialog默认头部
        Dialog dialog = getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(getCancelOutside());
        Window window = dialog.getWindow();
        if (null != window) {
            WindowManager.LayoutParams wlp = window.getAttributes();
            if (null != wlp) {
                if (getDialogX() != 0) {
                    wlp.x = getDialogX();
                }
                if (getDialogY() != 0) {
                    wlp.y = getDialogY();
                }
                window.setAttributes(wlp);
            }
            //设置弹窗启动动画
            if (getDialogAnimationRes() > 0) {
                window.setWindowAnimations(getDialogAnimationRes());
            }
        }
        dialog.setOnKeyListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            //设置窗体背景色透明
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //设置宽高
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = getDialogWidth();
            layoutParams.height = getDialogHeight();
            //透明度
            layoutParams.dimAmount = getDimAmount();
            //位置
            layoutParams.gravity = getGravity();
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            boolean keyBackDismiss = getKeyBackDismiss();
            if (null != getOnDialogKeyBackListener()) {
                getOnDialogKeyBackListener().onKeyBack();
            }
            return !keyBackDismiss;
        } else {
            return false;
        }
    }

    /**
     * 点击返回对话框是否消失
     *
     * @return
     */
    public boolean getKeyBackDismiss() {
        return KEY_BACK_DISMISS;
    }

    /**
     * 默认宽为包裹内容
     *
     * @return
     */
    public int getDialogWidth() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 默认高为包裹内容
     *
     * @return
     */
    public int getDialogHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 默认弹窗位置为屏幕中心
     *
     * @return
     */
    public int getGravity() {
        return Gravity.CENTER;
    }

    /**
     * 默认透明度为 0.2f
     *
     * @return
     */
    public float getDimAmount() {
        return DEFAULT_DIM;
    }

    /**
     * 默认点击对话框外部可以关闭对话框
     *
     * @return
     */
    public boolean getCancelOutside() {
        return true;
    }

    public String getFragmentTag() {
        return TAG;
    }

    /**
     * 获取弹窗显示动画, 子类实现
     */
    protected int getDialogAnimationRes() {
        return R.style.animate_bottom_dialog;
    }

    /**
     * 设置对话框主题
     *
     * @return
     */
    public int getDialogThemeStyleRes() {
        return R.style.CustomDialog;
    }

    public void show(FragmentManager fragmentManager) {
        String fragmentTag = getFragmentTag();
        if (null == fragmentManager || TextUtils.isEmpty(fragmentTag)) {
            return;
        }
        fragmentManager.executePendingTransactions();
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTag);
        //防止fragment重复添加导致崩溃
        if (fragment == null || !fragment.isAdded()) {
            show(fragmentManager, fragmentTag);
        }
    }

    public OnDialogKeyBackListener getOnDialogKeyBackListener() {
        return mOnDialogKeyBackListener;
    }

    /**
     * 获取对话框x坐标
     *
     * @return
     */
    public int getDialogX() {
        return 0;
    }

    /**
     * 获取对话框x坐标
     *
     * @return
     */
    public int getDialogY() {
        return 0;
    }

    public static interface OnDialogKeyBackListener {
        void onKeyBack();
    }
}
